package com.aaa.ddd.controller;

import com.aaa.fpa.controller.BaseController;
import com.aaa.pojo.Menu;
import com.aaa.pojo.MyReturn;
import com.aaa.utils.TreeUtil;
import com.aaa.ddd.service.MenuService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController extends BaseController {
    @Resource
    private MenuService menuService;

    private MyReturn myReturn= new MyReturn();

    @RequestMapping("toShowMenu.do")
    public ModelAndView toShowMenu(ModelAndView modelAndView) {
        modelAndView.setViewName("showMenu");
        return modelAndView;
    }

    @GetMapping({"selectOne.do"})
    public Menu selectOne(Integer id) {
        return this.menuService.queryById(id);
    }

    @GetMapping({"queryAllMenu.do"})
    public MyReturn queryAllMenu() {
        List<Menu> menus = menuService.queryAllMenu();

        myReturn.setData(menus);
        return getSuccessResult(myReturn);
    }

    @GetMapping({"queryAllMenus.do"})
    public List queryAllMenus() {
        List<Menu> menus = this.menuService.queryAllMenu();
        return TreeUtil.fromMenuListToTreeList(menus);
    }

    @RequestMapping({"deleteMenuById.do"})
    public MyReturn deleteMenuById(Integer menuId) {
        int i = this.menuService.deleteMenuById(menuId);
        return i > 0 ? super.getSuccessResult(this.myReturn) : super.getFailResult(this.myReturn);
    }

    @PostMapping({"insertMenu.do"})
    public MyReturn insertMenu(Menu menu) {
        if (menu.getParentId() == null) {
            menu.setParentId(0);
        }

        Menu insert = this.menuService.insert(menu);
        return insert != null ? super.getSuccessResult((MyReturn)null) : super.getFailResult(this.myReturn);
    }

    @RequestMapping({"updateMenu.do"})
    public MyReturn updateMenu(Menu menu) {
        try {
            Menu update = this.menuService.update(menu);
            this.myReturn.setData(update);
            return super.getSuccessResult(this.myReturn);
        } catch (Exception var3) {
            return super.getFailResult(this.myReturn);
        }
    }

    @GetMapping({"findParentNameId.do"})
    public MyReturn findParentNameId(Integer menuId) {
        Menu parent = this.menuService.findParentNameId(menuId);
        this.myReturn.setData(parent);
        return super.getSuccessResult(this.myReturn);
    }
}
